import json
from datetime import datetime

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from loguru import logger

from service import ai_studio, cloud_studio, tisp, wx
from service.attendance import get_user_statics

scheduler = AsyncIOScheduler()


## 文档链接   https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html
## day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
@scheduler.scheduled_job(CronTrigger.from_crontab("50 7 * * 0-4"))
async def cron_job():
	wx.push_multi("打卡提醒")


# @scheduler.scheduled_job(CronTrigger.from_crontab('00 17 * * 0-4'))
# async def cron_job_afternoon():
# 	wx.push_multi("下班打卡提醒")


@scheduler.scheduled_job(CronTrigger.from_crontab("55 7,17 * * 0-4"))
async def cron_remind():
	now = datetime.now()
	date_time = now.strftime("%Y-%m-%d")
	response_str = get_user_statics(date_time)
	content = json.loads(response_str)
	data = content["data"]
	logger.info("data = {}", data)
	if not data:
		return
	index = now.day - 1
	pm = now.hour > 12
	# cardFlag 0 未打卡   1 已打卡
	message = "下班打卡提醒" if pm else "上班打卡提醒"
	today_data = data[index]
	logger.info("today_data = {}", today_data)
	if today_data["normalList"][int(pm)]["cardFlag"] == "0":
		wx.push("😮😮😮😮😮忘记打卡", message)


@scheduler.scheduled_job(CronTrigger.from_crontab("*/2 7-23 * * *"))
async def cron_tisp():
	await tisp.visa_status_change()


@scheduler.scheduled_job(CronTrigger.from_crontab("* 0 * * *"))
async def cron_tisp_clear():
	tisp.sent_set.clear()


@scheduler.scheduled_job(CronTrigger.from_crontab("0 8 * * *"))
async def cron_ai_studio_check():
	await ai_studio.sign()


@scheduler.scheduled_job(CronTrigger.from_crontab("0 8 * * *"))
async def cron_cloud_studio_check():
	await cloud_studio.activity_task()
